package entity

import (
	"go_iot/apps/base"
	"time"
)

type Project struct {
	base.Base
	Name        string    `json:"name" validate:"required" gorm:"column:name"`
	Description *string   `json:"desc" gorm:"column:description"`
	UserId      int       `json:"user" gorm:"column:user_id"`
	Status      uint      `json:"status" gorm:"column:status"`
	CreatedAt   time.Time `json:"create" gorm:"column:created_at"`
	AccessKey   string    `json:"access" gorm:"column:accesskey"`
}

func (Project) TableName() string {
	return "projects"
}

func (p *Project) SearchMap() map[string]any {
	var m = map[string]any{}
	if p.UserId < 1 {
		return nil
	}
	m["user_id = ?"] = p.UserId
	if p.Name != "" {
		m["name like ?"] = "%" + p.Name + "%"
	}
	if p.AccessKey != "" {
		m["accesskey = ?"] = p.AccessKey
	}
	return m
}
